package com.quectel.sys.module.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.sys.constants.RedisCacheConstants;
import com.quectel.sys.module.sys.dao.SysMenuDao;
import com.quectel.sys.module.sys.dao.SysRoleMenuDao;
import com.quectel.sys.module.sys.dto.SysMenuDto;
import com.quectel.sys.module.sys.entity.SysMenuEntity;
import com.quectel.sys.module.sys.entity.SysRoleMenuEntity;
import com.quectel.sys.module.sys.service.SysMenuService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-04-02 09:42:45
 */
@DubboService
@Slf4j
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public SysMenuDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                RedisCacheConstants.SYS_MENU_BY_ID + id,
                5 * 60
        );
    }

    @Override
    public void clearByModuleId(Long moduleId) {
        sysMenuDao.clearByModuleId(moduleId);
    }


    @Override
    public List<SysMenuDto> queryList(Map<String, Object> params) {
        List<SysMenuEntity> list = sysMenuDao.queryList(params);
        return CopyUtils.copyList(list, SysMenuDto.class);
    }


    @Override
    public void save(SysMenuDto dto) {
        SysMenuEntity entity = CopyUtils.copyObj(dto, SysMenuEntity.class);
        entity.setId(Snowflake.nextId());
        sysMenuDao.insert(entity);
    }

    @Override
    public void updateById(SysMenuDto dto) {
        SysMenuEntity entity = CopyUtils.copyObj(dto, SysMenuEntity.class);
        sysMenuDao.updateById(entity);
    }

    @Override
    public SysMenuDto selectById(Long id) {
        SysMenuEntity entity = sysMenuDao.selectById(id);
        return CopyUtils.copyObj(entity, SysMenuDto.class);
    }

    @Override
    public void deleteById(Long id) {
        sysMenuDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteBatch(Long[] ids) {
        Map<String, Object> map = new HashMap<>();
        for (Long id : ids) {
            map.put("parent_id", id);
            List<SysMenuEntity> sysMenuEntities = this.sysMenuDao.selectByMap(map);
            if (sysMenuEntities != null && sysMenuEntities.size() > 0) {
                return "请先删除子目录";
            }
        }
        for (Long id : ids) {
            //删除角色与菜单的关系
            sysRoleMenuDao.delete(new QueryWrapper<SysRoleMenuEntity>().eq("menu_id", id));
            //删除菜单
            deleteById(id);
        }
        return null;
    }

}
